package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Page;
import org.kjtc.entity.RepairFauleHistory;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.RepairFauleHistoryService;
import org.kjtc.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BreakRepairRecordController extends BaseController{
	@Resource(name = "repairFauleHistoryService")
    private RepairFauleHistoryService repairFauleHistoryService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@RequestMapping(value = "/searchBreakRepairRecord")
	@ResponseBody
	public Map<String, Object> searchRepairFauleRecord(RepairFauleHistory repairFauleHistory,String page,String rows){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "故障维修记录查询开始");
		// 分页查询
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		// 调用service方法,取得充电集合
		List<RepairFauleHistory> repairFauleHistories = null;
		try {
			repairFauleHistories = repairFauleHistoryService.getRepairFauleHistoryPage(repairFauleHistory, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "故障维修记录查询异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt", pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("repairFauleHistoriesList", repairFauleHistories);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "故障维修记录查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "故障维修记录查询");
		repairFauleHistories = null;
		return map;
	}

	@RequestMapping(value = "/saveBreakRepairRecord")
	@ResponseBody
	public Map<String, Object> save(RepairFauleHistory repairFauleHistory){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "故障维修记录添加开始");
		repairFauleHistory.setRepairTime(Tools.str2DateTime(repairFauleHistory.getRepairDate()));
		repairFauleHistory.setReportTime(Tools.str2DateTime(repairFauleHistory.getReportDate()));
		repairFauleHistory.setSuccessTime(Tools.str2DateTime(repairFauleHistory.getSuccessDate()));
		repairFauleHistory.setCreateUser(this.getUesr().getUserID());
		repairFauleHistory.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = repairFauleHistoryService.insertRepairFauleHistory(repairFauleHistory);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
			} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "故障维修记录添加异常" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "故障维修记录添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "故障维修记录添加");
		repairFauleHistory = null;
		return map;
	}
}
